﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CMDBImportCIDLL
{
    public class Sheet
    {
        public string SheetName { get; set; }
        public string ModelName { get; set; }
        //三层
        public string Category { get; set; }
        public string Type { get; set; }
        public string Item { get; set; }
        //列信息
        public List<Column> ColumnList { get; set; }
    }
    public class Column
    {
        public int ColumnId { get; set; }
        public string ColumnName { get; set; }
        public string DBName { get; set; }
    }

    public class AnalyzeXML
    {
        private string path = System.AppDomain.CurrentDomain.BaseDirectory + "\\Mapping.xml";

        public List<Column> getExcelToDBColumnDic(string sheetname)
        {
            List<Column> list = new List<Column>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNode root = xmlDoc.DocumentElement;
            foreach (XmlNode file in root.ChildNodes)
            {
                XmlElement fe = (XmlElement)file;

                foreach (XmlNode sheet in file.ChildNodes)
                {
                    XmlElement se = (XmlElement)sheet;
                    if (se.GetAttribute("sheetname") == sheetname)
                    {
                        foreach (XmlNode column in sheet.ChildNodes)
                        {
                            Column dic = new Column();
                            dic.ColumnId = int.Parse(column.Attributes["columnid"].Value);
                            dic.ColumnName = column.Attributes["columnname"].Value;
                            dic.DBName = column.Attributes["dbcolumn"].Value;
                            list.Add(dic);
                        }
                    }
                }

            }
            return list;
        }

        public List<Sheet> getExcelToDBSheetNameDic()
        {
            List<Sheet> list = new List<Sheet>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNode root = xmlDoc.DocumentElement;
            foreach (XmlNode file in root.ChildNodes)
            {
                XmlElement fe = (XmlElement)file;

                foreach (XmlNode sheet in file.ChildNodes)
                {
                    XmlElement se = (XmlElement)sheet;
                    Sheet s = new Sheet();
                    s.SheetName = se.GetAttribute("sheetname");
                    s.ModelName = se.GetAttribute("modelname");
                    s.Category = se.GetAttribute("category");
                    s.Type = se.GetAttribute("type");
                    s.Item = se.GetAttribute("item");
                    s.ColumnList = getExcelToDBColumnDic(se.GetAttribute("sheetname"));
                    list.Add(s);
                }

            }
            return list;
        }
    }
}
